package eddie;

public class InputFrame extends javax.swing.JFrame {

    public InputFrame() {
        initComponents();
    }
    
    public static void main(String arhs[]){
        //new InputFrame().setVisible(true);
        new InputFrame().setVisible(true);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        tabpaneMathFunctions = new javax.swing.JTabbedPane();
        bsPanel = new javax.swing.JPanel();
        bsInputField = new javax.swing.JTextField();
        bsButton = new javax.swing.JButton();
        bsOutputLabel = new javax.swing.JLabel();
        fromBase = new javax.swing.JComboBox();
        toBase = new javax.swing.JComboBox();
        switchButton = new javax.swing.JButton();
        toLabel = new javax.swing.JLabel();
        pfPanel = new javax.swing.JPanel();
        pfInputField = new javax.swing.JTextField();
        pfButton = new javax.swing.JButton();
        pfOutputLabel = new javax.swing.JLabel();

        jLabel1.setText("jLabel1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        bsPanel.setPreferredSize(new java.awt.Dimension(360, 172));

        bsInputField.setToolTipText("");
        bsInputField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bsInputFieldActionPerformed(evt);
            }
        });

        bsButton.setText("Enter");
        bsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bsButtonActionPerformed(evt);
            }
        });

        fromBase.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Binary", "Octal", "Decimal", "Duodicemal", "Hexidecimal" }));
        fromBase.setSelectedIndex(2);

        toBase.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Binary", "Octal", "Decimal", "Duodicemal", "Hexidecimal" }));
        toBase.setSelectedIndex(4);

        switchButton.setText("Switch");
        switchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                switchButtonActionPerformed(evt);
            }
        });

        toLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        toLabel.setText("to");

        javax.swing.GroupLayout bsPanelLayout = new javax.swing.GroupLayout(bsPanel);
        bsPanel.setLayout(bsPanelLayout);
        bsPanelLayout.setHorizontalGroup(
            bsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(bsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(bsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(bsPanelLayout.createSequentialGroup()
                        .addComponent(bsOutputLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 215, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 44, Short.MAX_VALUE)
                        .addGroup(bsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(toBase, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(fromBase, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(switchButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, bsPanelLayout.createSequentialGroup()
                                .addComponent(toLabel)
                                .addGap(38, 38, 38))))
                    .addGroup(bsPanelLayout.createSequentialGroup()
                        .addComponent(bsInputField, javax.swing.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE)
                        .addGap(30, 30, 30)
                        .addComponent(bsButton, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        bsPanelLayout.setVerticalGroup(
            bsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, bsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(bsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(bsOutputLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(bsPanelLayout.createSequentialGroup()
                        .addComponent(fromBase, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(8, 8, 8)
                        .addComponent(toLabel)
                        .addGap(8, 8, 8)
                        .addComponent(toBase, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(switchButton)))
                .addGap(7, 7, 7)
                .addGroup(bsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bsInputField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bsButton))
                .addContainerGap())
        );

        tabpaneMathFunctions.addTab("Base Change", bsPanel);

        pfPanel.setPreferredSize(new java.awt.Dimension(360, 172));

        pfInputField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pfInputFieldActionPerformed(evt);
            }
        });

        pfButton.setText("Enter");
        pfButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pfButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pfPanelLayout = new javax.swing.GroupLayout(pfPanel);
        pfPanel.setLayout(pfPanelLayout);
        pfPanelLayout.setHorizontalGroup(
            pfPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pfPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pfPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pfPanelLayout.createSequentialGroup()
                        .addComponent(pfInputField, javax.swing.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE)
                        .addGap(30, 30, 30)
                        .addComponent(pfButton, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(pfPanelLayout.createSequentialGroup()
                        .addComponent(pfOutputLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 215, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(25, 25, 25))))
        );
        pfPanelLayout.setVerticalGroup(
            pfPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pfPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pfOutputLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
                .addGroup(pfPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(pfInputField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(pfButton))
                .addContainerGap())
        );

        tabpaneMathFunctions.addTab("Prime Factor", pfPanel);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(tabpaneMathFunctions)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(tabpaneMathFunctions)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void pfInputFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pfInputFieldActionPerformed
        setupFactor();
    }//GEN-LAST:event_pfInputFieldActionPerformed

    private void pfButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pfButtonActionPerformed
        setupFactor();
    }//GEN-LAST:event_pfButtonActionPerformed

    private void bsInputFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bsInputFieldActionPerformed
         setupBase();
    }//GEN-LAST:event_bsInputFieldActionPerformed

    private void switchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_switchButtonActionPerformed
        int x = fromBase.getSelectedIndex();
        int y = toBase.getSelectedIndex();
        fromBase.setSelectedIndex(y);
        toBase.setSelectedIndex(x);
    }//GEN-LAST:event_switchButtonActionPerformed

    private void bsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bsButtonActionPerformed
         setupBase();
    }//GEN-LAST:event_bsButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bsButton;
    private javax.swing.JTextField bsInputField;
    private javax.swing.JLabel bsOutputLabel;
    private javax.swing.JPanel bsPanel;
    private javax.swing.JComboBox fromBase;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JButton pfButton;
    private javax.swing.JTextField pfInputField;
    private javax.swing.JLabel pfOutputLabel;
    private javax.swing.JPanel pfPanel;
    private javax.swing.JButton switchButton;
    private javax.swing.JTabbedPane tabpaneMathFunctions;
    private javax.swing.JComboBox toBase;
    private javax.swing.JLabel toLabel;
    // End of variables declaration//GEN-END:variables

    public void setupBase(){
        
        int base1 = 0;
        int base2 = 0;
        
        input = bsInputField.getText();
        bsInputField.setText("");
        if (input == null)input=" ";
     
        switch (fromBase.getSelectedIndex()){
            case 0: base1 = 2;break;
            case 1: base1 = 8;break;
            case 2: base1 = 10;break;
            case 3: base1 = 12;break;
            case 4: base1 = 16;break;
        }
        switch (toBase.getSelectedIndex()){
            case 0: base2 = 2;break;
            case 1: base2 = 8;break;
            case 2: base2 = 10;break;
            case 3: base2 = 12;break;
            case 4: base2 = 16;break;
        }
        bsOutputLabel.setText(Algorithms.changeBase(input, base1, base2));
      //outputField.setText(Algorithms.primeFactor(Double.parseDouble(input)));
          
    }
    
    public void setupFactor(){
        input = pfInputField.getText();
        pfInputField.setText("");
        try{
            pfOutputLabel.setText(Algorithms.primeFactor(Double.parseDouble(input)));
        } catch (Exception e){
            pfOutputLabel.setText("Not a valid number");
        }
    }
    
    String input;
    int base1 = 0;
    int base2 = 0;

}
